import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedList;
import java.util.Random;


public class kxxxl implements ActionListener {
    ImageIcon[] icon = new ImageIcon[6];
    ImageIcon bai = new ImageIcon();
    Path imagePath;

    {
        imagePath = Paths.get("tupian", "zi(1).png");
        icon[0] = new ImageIcon(imagePath.toAbsolutePath().toString());
        imagePath = Paths.get("tupian", "cheng(1).png");
        icon[1] = new ImageIcon(imagePath.toAbsolutePath().toString());
        imagePath = Paths.get("tupian", "huang(1).png");
        icon[2] = new ImageIcon(imagePath.toAbsolutePath().toString());
        imagePath = Paths.get("tupian", "hong(1).png");
        icon[3] = new ImageIcon(imagePath.toAbsolutePath().toString());
        imagePath = Paths.get("tupian", "lan(1).png");
        icon[4] = new ImageIcon(imagePath.toAbsolutePath().toString());
        imagePath = Paths.get("tupian", "lv(1).png");
        icon[5] = new ImageIcon(imagePath.toAbsolutePath().toString());
        imagePath = Paths.get("tupian", "bai(1).png");
        bai = new ImageIcon(imagePath.toAbsolutePath().toString());
    }


    public  static boolean[] vis=new boolean[100];
    int[] colors1 = {255, 255, 250, 124, 19,255};
    int[] colors2 = {77, 122, 173, 179, 194,100};
    int[] colors3 = {79, 69, 20, 5, 194,150};
    public static JButton[] buttons= new JButton[100];
    public static Integer[][] fx= {{-1,0},{1,0},{0,1},{0,-1}};
    public static JLabel a;
    public static JLabel b;
    public static JFrame frame;
    public static int end=10;
    public  kxxxl (){
        SwingUtilities.invokeLater(() -> {
            frame = new JFrame();
            frame.setSize(700, 780); // 增加高度以容纳标签
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);

            // 使用BorderLayout管理整个窗口
            frame.setLayout(new BorderLayout());
            JPanel tpanel = new JPanel();
            // 创建顶部标签
            a = new JLabel("Defense:0");
            a.setHorizontalAlignment(JLabel.CENTER);
            a.setFont(new Font("Arial", Font.BOLD, 16));
            a.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));
            a.setBackground(Color.LIGHT_GRAY);
            a.setOpaque(true);

            b = new JLabel("timer:60");
            b.setHorizontalAlignment(JLabel.CENTER);
            b.setFont(new Font("Arial", Font.BOLD, 16));
            b.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));
            b.setBackground(Color.LIGHT_GRAY);
            b.setOpaque(true);
            tpanel.add(a);
            tpanel.add(b);
            // 创建10x10网格布局的面板
            JPanel panel = new JPanel(new GridLayout(10, 10, 1, 1));


            // 定义可用颜色


            Random random = new Random();

            // 创建100个随机颜色的按钮
            for (int i = 0; i < 100; i++) {
                int ind = random.nextInt(colors1.length);
                buttons[i] = new JButton(icon[ind]);
                buttons[i].setBackground(new Color(colors1[ind], colors2[ind], colors3[ind]));
                buttons[i].setBackground(Color.white);
                buttons[i].setText(String.valueOf(i));
                buttons[i].setFont(new Font("Arial", Font.BOLD, 0));
                buttons[i].setOpaque(true);
                buttons[i].setBorder(new LineBorder(Color.white, 0));
                buttons[i].addActionListener(this);
                panel.add( buttons[i]);
            }

            // 将标签添加到顶部，面板添加到中央
            frame.add(tpanel, BorderLayout.NORTH);
            frame.add(panel, BorderLayout.CENTER);

            frame.setVisible(true);
        });


    }

    public void jf(int s){
        String text= (a.getText().substring(8));
        int ss= Integer.parseInt(text)+s*5*s;
        a.setText("Defense:"+ ss);
    }

    public void init(){

        for (int i = 0; i < 100; i++) {
            Random random = new Random();
            int ind = random.nextInt(colors1.length);
            buttons[i].setBackground(new Color(colors1[ind], colors2[ind], colors3[ind]));

            a.setText("Defense:0");
        }
        end=10;

    }

    public static void main(String[] args) {

        new kxxxl();

        for(int i=59;i>=0;i--){
            try {
                Thread.sleep(1000);
                b.setText("time: " + i);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        System.out.println("gameover");
        int yh=JOptionPane.showConfirmDialog(null,
                "时间到","游戏结束",
                JOptionPane.OK_CANCEL_OPTION,
                JOptionPane.QUESTION_MESSAGE);
    }

    public void isjs(){
        boolean f =false;
        for(int z = 0; z <100; z++){
            if(!(buttons[z].getIcon().equals(bai))){
                int x= z /10;
                int y= z -(x*10);
                System.out.println(x+"||"+y);
                for(int i=0;i<4;i++){
                    int nx=x+fx[i][0];
                    int ny=y+fx[i][1];
                    if(nx<0 || nx>=10 || ny<0 || ny>=10){
                        continue;
                    }
                    int zhi=(nx*10+ny);
                    if(buttons[zhi].getIcon().equals(buttons[z].getIcon())){
                        f=true;
                    }
                }
            }
        }
        if(!f){
            String text= (a.getText().substring(8));
            System.out.println("gameover");
            int yh=JOptionPane.showConfirmDialog(null,
                    "没有可消除方块了","游戏结束",
                    JOptionPane.OK_CANCEL_OPTION,
                    JOptionPane.QUESTION_MESSAGE);
            if(yh==0){
                init();
            }else{
                System.exit(0);
            }

        }
    }
    public void tcfk(){
        for(int i=0;i<100;i++){
            if(buttons[i].getIcon().equals(bai)){
                Random random = new Random();
                int ind = random.nextInt(colors1.length);
                buttons[i].setIcon(icon[ind]);
            }
        }
        isjs();
    }


    public void  fkdl(){

        while(true){
            boolean f=true;
            for(int i=0;i<90;i++){
                if(buttons[i].getIcon().equals(bai)){
                    continue;
                }
                if(buttons[i+10].getIcon().equals(bai)){
                    f=false;
                    Icon color = buttons[i].getIcon();
                    buttons[i].setIcon(bai);
                    buttons[i+10].setIcon(color);
                }
            }
            if(f){
                break;
            }
        }

        while(true){
            boolean ff =true;
            for(int x=0;x<end;x++){
                boolean f=true;
                for(int y=0;y<100;y=y+10){
                    if(!buttons[x+y].getIcon().equals(bai)){
                        f=false;
                    }
                }
                if(f){
                    for(int xx=x;xx<end-1;xx++){
                        for (int yy=0;yy<100;yy+=10){
                            if(xx+1==end-1){
                                buttons[xx+yy].setIcon(buttons[xx+1+yy].getIcon());
                                buttons[xx+1+yy].setIcon(bai);
                            }else{
                                buttons[xx+yy].setIcon(buttons[xx+1+yy].getIcon());
                            }

                        }
                    }
                    end--;
                    ff=false;
                }
            }
            if(ff){
                break;
            }
        }

        tcfk();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String s = e.getActionCommand();

        int shu = Integer.parseInt(s);
        System.out.println(shu);
        if(buttons[shu].getIcon().equals(bai)){
            return;
        }
        LinkedList<Integer >list1 = new LinkedList<Integer>();
        LinkedList<Integer >list2 = new LinkedList<Integer>();
        list1.add(shu);
        list2.add(shu);
        //fx= {{-1,0},{1,0},{0,1},{0,-1}};
        boolean[] vis=new boolean[100];
        for(int i=0;i<100;i++){
            vis[i]=false;
        }
        vis[shu]=true;
        while(!list1.isEmpty()){
            int a=list1.getLast();
            int x=a/10;
            int y=a-(x*10);
            list1.removeLast();
            System.out.println(x+"||"+y);

            for(int i=0;i<4;i++){
                int nx=x+fx[i][0];
                int ny=y+fx[i][1];

                if(nx<0 || nx>=10 || ny<0 || ny>=10 || vis[nx*10+ny]){
                    continue;
                }
                int zhi=(nx*10+ny);
                vis[zhi]=true;
                System.out.println("dqdqdqdq:"+zhi);


                System.out.println(buttons[nx*10+ny].getBackground().equals(buttons[a].getBackground()));
                if(buttons[nx*10+ny].getIcon().equals(buttons[a].getIcon())){
                    list1.add(zhi);
                    list2.add(nx*10+ny);
                }

            }


        }
        System.out.println("选中方块"+list2.size());
        for (int z:list2){
            System.out.println(z);
        }
        if(list2.size()>=2){
            //shanchu
            for (int ys:list2){
                buttons[ys].setIcon(bai);
            }
            fkdl();
            jf(list2.size());
            isjs();
        }
    }
}
